<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends MX_Controller {

    function __construct() {
        parent::__construct ();
		$this->load->model('admin_model');
    }

    protected function _eventLoadLayout() {
        $this->page ['title'] = 'Bảng điều khiển';
        $this->module ['title'] = 'Bảng điều khiển';
    }

    public function authenticate() {
        $this->_clear_logged_session();
        if (!isset($_SESSION)) {
            echo 'Session Not Start, Please Validate Configuration';
        }
        $this->load->view('login/form');
    }

    public function login() {
		$this->_clear_logged_session();
        $username = $this->input->post('username');
        $password = $this->input->post('password');
		$session_key = $this->input->post('session_key');
        $data = $this->admin_model->login($username, $password);
        if (!empty($data)) {
            $logged_data = array('admin' => $data, 'logged_in' => TRUE, 'session_key' => $session_key, 'admin_username' => $username, 'admin_id' => $data->id);
			$this->session->set_userdata($logged_data);
			$this->st_registry->register('admin/login/success', 'Đăng nhập thành công');
            redirect(site_url("admin/cms/category/index", true));
        } else {
			$logged_data = array('admin' => null, 'session_key' => null, 'logged_in' => FALSE);
			$this->session->set_userdata($logged_data);
            $this->st_registry->register('admin/login/error', 'Tên đăng nhập không tồn tại hoặc mật khẩu sai');
            redirect(site_url("admin/authenticate"));
        }
    }

    public function logout() {
        $this->_clear_logged_session();
        $this->st_registry->register('admin/success', 'Đăng xuất thành công');
        redirect(site_url("admin/authenticate"));
    }

    public function dashboard() {
        try {
            $this->load->model('catalog_category_model');
            $this->load->model('catalog_product_model');
            $this->load->model('cms_category_model');
            $this->load->model('cms_article_model');
            $data['categories'] = $this->catalog_category_model->get_collection(0, 10);
            $data['total_categories'] = $this->catalog_category_model->count_total();
            $data['products'] = $this->catalog_product_model->get_collection(0, 10, null, null);
            $data['total_products'] = $this->catalog_product_model->count_total();
            $data['cms_categories'] = $this->cms_category_model->get_collection(0, 10);
            $data['total_cms_categories'] = $this->cms_category_model->count_total();
            $data['articles'] = $this->cms_article_model->get_collection(0, 10, null, null);
            $data['total_articles'] = $this->cms_article_model->count_total();
            $this->setPageContent('admin/dashboard', $data);
            $this->renderLayout('layouts/fullcolumn');
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }
	
	private function _clear_logged_session() {
		$logged_data = array('admin' => null, 'session_key' => null, 'logged_in' => FALSE, 'admin_username' => null, 'admin_id' => 0);
        $this->session->set_userdata($logged_data);
		return $this;	
	}

}
